<template>
  <el-container>
    <BomTree style="margin-top: 16px" @on-choose-region="filetrStation" />
    <el-main>
      <div class="default-main ba-table-box">
        <TableHeader
          :buttons="[
            'refresh',
            'add',
            'edit',
            'remove',
            'comSearch',
            'columnDisplay',
            'upload',
            'export',
          ]"
          :quick-search-placeholder="placeholder"
          @action="baTable.onTableHeaderAction"
        />
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table
          ref="tableRef"
          :pagination="false"
          @action="baTable.onTableAction"
        />
        <!-- 表单 -->
        <PopupForm
          v-if="
            (baTable.form.operate !== 'upload' &&
              JSON.stringify(bom.bomInfo) !== '{}' &&
              baTable.form.operate) ||
            baTable.form.operate === 'edit' ||
            baTable.form.operate === 'itemAdd'
          "
        />
        <!-- 上传excel文件 -->
        <Upload
          v-if="baTable.form.operate && baTable.form.operate === 'upload'"
        />
      </div>
    </el-main>
  </el-container>
</template>
<script lang="ts" setup>
import {
  ref,
  provide,
  onMounted,
  markRaw,
  reactive,
  onBeforeUnmount,
} from "vue";
import baTableClass from "@/utils/baTable";
import { baTableApi, operatorShowOff } from "@/utils/common"; // 导入表格api方法生成器
import { BomMRoute } from "@/api/backend/index";
import Table from "@/components/table/index.vue";
import TableHeader from "@/components/table/header/index.vue";
import PopupForm from "./popupForm.vue";
import Upload from "./components/import.vue";
import { useRoute } from "vue-router";
import { useNavTabs } from "@/store/navTabs";
import type { TableRow, TableColumn } from "@/utils/baTable"; //表格列行类型
import { useDictionary } from "@/store/backend/tool/dictionary";
import { useFactoryEquipment } from "@/store/backend/FactoryM/equipmentUnit";
import BomTree from "./components/aside.vue";
import { useFactoryStation } from "@/store/backend/FactoryM/stationInfo";
import { useBomM } from "@/store/backend/BomM/bomM";
import { useBomRoute } from "@/store/backend/BomM/bomRoute";
import useCurrentInstance from "@/utils/useCurrentInstance";
import { useBomProcess } from "@/store/backend/BomM/bomProcess";
import TagVue from "./components/tag.vue";
import { useDept } from "@/store/backend/SysM/dept";

const placeholder = ref<string>("模糊搜索");
const tableRef = ref();
const route = useRoute();
const navTabs = useNavTabs();
const dictionary = useDictionary();
const equipUnit = useFactoryEquipment();
const station = useFactoryStation();
const bom = useBomM();
const bomR = useBomRoute();
const bomP = useBomProcess();
const dept = useDept();

//字段显隐记忆功能
const filedShowOff = (filter: any) => {
  if ((navTabs as any).columnDisplayStoreMap) {
    return (navTabs as any).columnDisplayStoreMap.get(
      route.path + "/" + filter
    );
  }
  return true;
};
// 每页个数记忆功能 默认10个
const pageSizeKeep = () => {
  if ((navTabs as any).pageSizeMap) {
    return (navTabs as any).pageSizeMap.get(route.path + BomMRoute)
      ? (navTabs as any).pageSizeMap.get(route.path + BomMRoute)
      : 10;
  }
  return 10;
};

const baTable = new baTableClass(
  new baTableApi(BomMRoute),
  {
    pk: "id", //区域ID
    column: [
      { type: "selection", align: "center", operator: false },
      {
        show: filedShowOff("materialCode"),
        label: "物料编码",
        prop: "materialCode",
        align: "left",
      },
      // {
      //   show: filedShowOff("process"),
      //   label: "工序号",
      //   prop: "process",
      //   align: "left",
      // },
      {
        show: filedShowOff("processName"),
        label: "工序名称",
        prop: "processName",
        align: "left",
      },
      {
        show: filedShowOff("workshopCode"),
        label: "车间代码",
        prop: "workshopCode",
        align: "left",
      },
      {
        show: filedShowOff("workshopName"),
        label: "车间名称",
        prop: "workshopName",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("type"),
        label: "工序类型",
        prop: "type",
        align: "left",
        render: "customRender",
        customRender: markRaw(TagVue),
        operator: false,
      },
      {
        show: filedShowOff("perUsage"),
        label: "每用量",
        prop: "perUsage",
        align: "left",
        operatorPlaceholder: "模糊搜索",
      },
      {
        show: filedShowOff("prepareTime"),
        label: "单件额定准备时间",
        prop: "prepareTime",
        align: "left",
        operator: false,
      },
      {
        show: filedShowOff("productTime"),
        label: "单件额定加工时间",
        prop: "productTime",
        align: "left",
        operator: false,
      },
      {
        show: filedShowOff("equipmentCode"),
        label: "设备编号",
        prop: "equipmentCode",
        align: "left",
        operator: false,
      },
      {
        show: operatorShowOff([
          route.path + "/edit",
          route.path + "/add",
          route.path + "/remove",
        ]),
        label: "操作",
        align: "center",
        width: "130",
        operator: false,
        render: "buttons",
        fixed: "right",
        buttons: [
          {
            render: "tipButton",
            name: "edit",
            title: "编辑",
            text: "",
            type: "primary",
            icon: "iconfont icon-pencil",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "itemAddButton",
            name: "itemAdd", //与add绑定
            title: "新增",
            text: "",
            type: "primary",
            icon: "iconfont icon-xinzeng",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
          {
            render: "tipButton",
            name: "info",
            title: "详情",
            text: "",
            type: "primary",
            icon: "iconfont icon-chaxunyujisuan",
            class: "table-row-edit",
            disabledTip: false,
            // 按钮是否显示，请返回布尔值
            display: (row: TableRow, field: TableColumn) => {
              return true;
            },
            // 按钮是否禁用，请返回布尔值
            disabled: (row: TableRow, field: TableColumn) => {
              return false;
            },
            attr: {},
          },
        ],
      },
    ],
    filter: {
      pageNum: 1,
      pageSize: pageSizeKeep(),
    },
    extend: {
      maxHeight: 580,
    },
  },
  {
    defaultItems: {},
  }
);

//接收工作中心树传递过来的数据
const filetrStation = (row: any, node: any) => {
  console.log("接收bom树传递过来的数据");

  if (node.checkedKeys.length == 0) {
    //表示取消选择直接调用刷新表单方法
    // baTable.comSearch.form = Object.assign(baTable.comSearch.form, {
    //   regions: region.regionIds + "",
    // });
    // baTable.onTableHeaderAction("refresh", {});
    bom.$patch((state: any) => {
      state.bomInfo = {};
    });
  } else {
    baTable.comSearch.form.materialCode = row.code;
    baTable.comSearch.form.materialType = row.type;
    if (node.halfCheckedNodes.length > 0) {
      baTable.comSearch.form.parentCode = node.halfCheckedNodes[0].code;
      //如果有父级node就保存起来
      bomR.$patch((state: any) => {
        state.bomTreeParent = node.halfCheckedNodes[0];
      });
    } else {
      baTable.comSearch.form.parentCode = "";
      bomR.$patch((state: any) => {
        state.bomTreeParent = {};
      });
    }
    baTable.getList(BomMRoute);
    bom.$patch((state: any) => {
      state.bomInfo = row;
    });
  }
};

onMounted(() => {
  const { proxy } = useCurrentInstance();

  //将table的ref给到table类
  baTable.table.ref = tableRef.value;
  baTable.mount();
  baTable.getList(BomMRoute);

  /**
   * 表格内的按钮响应
   * @param name 按钮name
   * @param row 被操作行数据
   */
  proxy.eventBus.on(
    "onTableButtonClick",
    (data: { name: string; row: TableRow }) => {}
  );
});

onBeforeUnmount(() => {
  bom.$patch((state: any) => {
    state.bomInfo = {};
  });
});
// 实例化表格后，将 baTable 的实例提供给上下文
provide("baTable", baTable);
provide("dictionary", dictionary);
provide("equipUnit", equipUnit);
provide("station", station);
provide("bom", bom);
provide("bomR", bomR);
provide("bomP", bomP);
provide("dept", dept);
</script>
<style lang="scss" scoped>
.default-main {
  margin: 0px !important;
}
</style>
